import 'dart:ui' as ui;

import 'package:cc_sdk/models/comment_model.dart';
import 'package:cc_sdk/services/comment_service.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';

class CommentReplyWidget extends StatefulWidget {
  final int parentId;
  final String outId;
  final int memberId;
  TextEditingController controller;
  Function(CommentModel model) callback;

  CommentReplyWidget({Key key,
    @required this.parentId,
    @required this.outId,
    @required this.memberId,
    this.controller,
    this.callback})
      : super(key: key) {
    if (this.controller == null) this.controller = TextEditingController();
    if (this.callback == null) this.callback = (model) {};
  }

  @override
  State<CommentReplyWidget> createState() =>
      _CommentReplyWidgetState(
          this.parentId, this.outId, this.controller, this.callback);
}

class _CommentReplyWidgetState extends State<CommentReplyWidget> {
  final int parentId;
  final String outId;
  final TextEditingController controller;
  final void Function(CommentModel model) callback;
  bool sending = false;
  double keyboardHeight = 0;
  double viewInsetsBottom = 0;

  _CommentReplyWidgetState(this.parentId, this.outId, this.controller,
      this.callback);

  @protected
  void initState() {
    super.initState();

    KeyboardVisibilityNotification().addNewListener(
      onChange: (bool visible) {
        var win = WidgetsBinding.instance.window;
        var data = MediaQueryData.fromWindow(win);
        if (visible) {
          keyboardHeight = data.viewInsets.bottom - viewInsetsBottom;
        } else {
          viewInsetsBottom = data.viewInsets.bottom;
          keyboardHeight = 0;
        }
        setState(() {

        });
        print(
            "KeyboardVisibilityNotification:$keyboardHeight, $visible ,$data");
      },
    );
  }

  Future reply() async {
    if (sending) return;
    setState(() {
      sending = true;
    });
    String context = this.controller.text;
    if (context.isEmpty) {
      Fluttertoast.showToast(msg: "请输入评论");
    } else {
      try {
        var model = await CommentService.internal()
            .comment(outId, parentId, widget.memberId, context);
        this.callback(model);
      } on Exception catch (ex) {
        print(ex);
      }
    }
    setState(() {
      sending = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    var data = MediaQuery.of(context);
    viewInsetsBottom = data.viewInsets.bottom;
    var screenSize = data.size;
    return Container(
        height: screenSize.height,
        alignment: Alignment.bottomCenter,
        child: Column(
          children: <Widget>[
            Expanded(
              child: GestureDetector(
                child: Container(
                  color: Colors.transparent,
                ),
                onTap: () => this.callback(null),
              ),
            ),
            Container(
              width: screenSize.width,
              margin: EdgeInsets.only(bottom: keyboardHeight),
              child: Stack(
                children: <Widget>[
                  Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Color(0x7f000000),
                          offset: Offset(0, 0),
                          blurRadius: 10,
                          spreadRadius: 2,
                        ),
                      ],
                    ),
                    height: 2,
                  ),
                  Container(
                    padding: EdgeInsets.all(5),
                    decoration: BoxDecoration(
                      color:
                      Color.fromARGB((255 * 0.97).toInt(), 255, 255, 255),
                      border: Border(
                        top: BorderSide(color: Color(0xffc9c9c9), width: 0.3),
                      ),
                    ),
                    child: Row(
                      children: <Widget>[
                        Container(
                          constraints: BoxConstraints(
                            maxHeight: 144.0,
                            maxWidth: screenSize.width - 120,
                          ),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey),
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                          padding: EdgeInsets.only(
                              left: 16.0, right: 16.0, top: 8.0, bottom: 4.0),
                          child: TextField(
                            textInputAction: TextInputAction.send,
                            controller: this.controller,
                            maxLines: null,
                            autofocus: true,
                            keyboardType: TextInputType.multiline,
                            maxLength: 100,
                            decoration: InputDecoration.collapsed(
                              hintText: "输入评论内容",
                            ),
                            onSubmitted: (_) => reply(),
                          ),
                        ),
                        Container(
                          width: 110,
                          height: 50,
                          alignment: Alignment.center,
                          child: FlatButton.icon(
                            label: Text(
                              '发布',
                              style: TextStyle(color: Colors.white),
                            ),
                            color: Colors.blue,
                            icon: sending
                                ? SizedBox(
                              child: CircularProgressIndicator(
                                  backgroundColor: Colors.white),
                              height: 20.0,
                              width: 20.0,
                            )
                                : Icon(
                              Icons.send,
                              color: Colors.white,
                            ),
                            onPressed: reply,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ));
  }
}
